package com.jzit168.library_base.ext

import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText

/**
 *@Desc:
 *@Author: Android Developer
 *@Date: 2020/10/16
 *
 **/

fun EditText.addTextChanged(
    before: ((s: CharSequence?, start: Int, count: Int, after: Int) -> Unit)? = null,
    on: ((s: CharSequence?, start: Int, before: Int, count: Int) -> Unit)? = null,
    after: ((s: Editable?) -> Unit)? = null
) {
    addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            before?.invoke(s, start, count, after)
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            on?.invoke(s, start, before, count)
        }

        override fun afterTextChanged(s: Editable?) {
            after?.invoke(s)
        }
    })
}

fun EditText.isEmpty() = text.isNullOrEmpty()

fun EditText.setString() = text.toString().trim()

//如果输入的内容为空,返回null
fun EditText.setStringOrNull() = if (isEmpty()) null else setString().trim()